//
//  HZcomposeBottomView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/17.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit
// 底部视图的枚举
enum HZcomposeBottomViewType: Int {
    // 图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 😊
    case Emoticon = 3
    // +
    case Add = 4
}


class HZcomposeBottomView: UIView {

    var emoticonBtn:UIButton?
    
    
    // 定义一个属性供外界设置 (判断是否是表情键盘)
    var isEmoticon: Bool = false{
        
        didSet{
            /*
                true 代表是表情键盘 显示键盘图片
                false 代表系统键盘  显示笑脸图片
             */
            let imageName: String
            if isEmoticon {
                // 键盘图片
                imageName = "compose_keyboardbutton_background"
            }else{
                // 笑脸图片
                imageName = "compose_emoticonbutton_background"
            }
            
            emoticonBtn?.setImage(UIImage(named: "\(imageName)"), forState: UIControlState.Normal)
            emoticonBtn?.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    // 声明闭包 01
    var closure: ((type: HZcomposeBottomViewType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 按钮点击事件
    func buttonClick(button: UIButton){
        // 03 执行闭包  
        closure?(type: HZcomposeBottomViewType(rawValue: button.tag)!)
    }

    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        // 添加按钮
        let pictureBtn = addChildButton("compose_toolbar_picture",type: .Picture)
        let mentionBtn = addChildButton("compose_mentionbutton_background",type: .Mention)
        let trendBtn = addChildButton("compose_trendbutton_background",type: .Trend)
        emoticonBtn = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        let addBtn = addChildButton("compose_add_background",type: .Add)
        
        // 添加约束
        pictureBtn.snp_makeConstraints(closure: { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        })
        mentionBtn.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBtn.snp_trailing)
            make.width.equalTo(trendBtn)
        })
        trendBtn.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionBtn.snp_trailing)
            make.width.equalTo(emoticonBtn!)
        })
        emoticonBtn!.snp_makeConstraints(closure: { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendBtn.snp_trailing)
            make.width.equalTo(addBtn)
        })
        addBtn.snp_makeConstraints(closure: { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonBtn!.snp_trailing)
        })
        
        
    }
    //compose_add_background_highlighted
    func addChildButton(imageName: String, type: HZcomposeBottomViewType)-> UIButton{
        let btn = UIButton()
        // 设置tag  rawValue 通过枚举名 得到枚举值
        btn.tag = type.rawValue
        btn.addTarget(self, action: #selector(HZcomposeBottomView.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(btn)
        return btn
    }
}
